package com.adjaisd.scarecrow.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class Pager<T> implements Serializable {

    private long counts;    // 总条数

    private long pageSize;  // 每页条数

    private long pages;     // 总页数

    private long page;      // 当前页数

    private List<T> items;  // 当前页数据

    public Pager(IPage<T> page) {
        this.pageSize = page.getSize();
        this.counts = page.getTotal();
        this.page = page.getCurrent();
        this.pages = page.getPages();
        this.items = page.getRecords();
    }

    public Pager(Long counts, Long pageSize) {
        this.counts = counts;
        this.pageSize = pageSize;
        if (pageSize <= 0) pages = 0;
        else pages = counts % pageSize == 0 ? (counts / pageSize) : (counts / pageSize) + 1;
    }

    public void addPage(Long counts, List<T> items) {
        this.counts += counts;
        this.items.addAll(items);
        if (pageSize <= 0) pages = 0;
        else pages = counts % pageSize == 0 ? (counts / pageSize) : (counts / pageSize) + 1;
    }

}
